package com.localjoycommunity.cpdj.project.system.controller;

import java.util.List;

import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.project.system.domain.SysNews;
import com.localjoycommunity.cpdj.project.system.domain.UserData;
import com.localjoycommunity.cpdj.project.system.service.IUserDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.project.system.domain.SysLearnData;
import com.localjoycommunity.cpdj.project.system.service.ISysLearnDataService;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 学习资料Controller
 *
 * @author leepandar
 * @date 2020-10-30
 */
@RestController
@RequestMapping("/system/data" )
public class SysLearnDataController extends BaseController {
    @Autowired
    private ISysLearnDataService sysLearnDataService;
    @Autowired
    private IUserDataService userDataService;

    /**
     * 用户收藏学习资料
     */
    @NeedLogin
    @PostMapping("digData")
    public AjaxResult digNews(@RequestBody UserData userData, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userData.setUserId(userId);
        int i = userDataService.dataDig(userData);
        return AjaxResult.success(i);
    }

    /**
     * 查询学习资料列表
     */
    @GetMapping("/list" )
    public TableDataInfo list(SysLearnData sysLearnData) {
        startPage();
        List<SysLearnData> list = sysLearnDataService.selectSysLearnDataList1(sysLearnData);
        return getDataTable(list);
    }

    /**
     * 导出学习资料列表
     */
    @Log(title = "学习资料" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(SysLearnData sysLearnData) {
        List<SysLearnData> list = sysLearnDataService.selectSysLearnDataList(sysLearnData);
        ExcelUtil<SysLearnData> util = new ExcelUtil<SysLearnData>(SysLearnData.class);
        return util.exportExcel(list, "data" );
    }

    /**
     * 获取学习资料详细信息
     */
    @GetMapping(value = "/{learnId}" )
    public AjaxResult getInfo(@PathVariable("learnId" ) Long learnId) {
        return AjaxResult.success(sysLearnDataService.selectSysLearnDataById(learnId));
    }

    /**
     * 获取学习资料详细信息app
     */
    @GetMapping(value = "/app/{learnId}" )
    public AjaxResult getInfoApp(@PathVariable("learnId" ) Long learnId, HttpServletRequest req) {
        SysLearnData sysLearnData = sysLearnDataService.selectSysLearnDataByIdApp(learnId);
        Integer userId = getUserId(req);
        if (userId != 0){
            Integer dig = userDataService.isDig(userId, learnId);
            sysLearnData.setIsDig(dig);
        }

        return AjaxResult.success(sysLearnData);
    }

    /**
     * 新增学习资料
     */
    @Log(title = "学习资料" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysLearnData sysLearnData) {
        return toAjax(sysLearnDataService.insertSysLearnData(sysLearnData));
    }

    /**
     * 修改学习资料
     */
    @Log(title = "学习资料" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysLearnData sysLearnData) {
        return toAjax(sysLearnDataService.updateSysLearnData(sysLearnData));
    }

    /**
     * 删除学习资料
     */
    @Log(title = "学习资料" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{learnIds}" )
    public AjaxResult remove(@PathVariable Long[] learnIds) {
        return toAjax(sysLearnDataService.deleteSysLearnDataByIds(learnIds));
    }
}
